/**
 * Created by 1 on 2017/1/3.
 */
(function(){
    "use strict";
    function Timer(){
        this.time=0;
        this.canvas=new Canvas("timer",100,70);
        this.timerId;
        this._init();
    }
    Timer.prototype={
        constructor:Timer,
        _init:function(){
            this._render();
            this.resume();
        },
        _render:function(){
            this.canvas.drawText(this._format(this.time));
        },
        _format:function(seconds){
            var hours=Math.floor(seconds / 3600);
            seconds=seconds-hours*3600;
            var minutes=Math.floor(seconds / 60);
            seconds=seconds-minutes*60;
            if(hours<10){
                hours="0"+hours;
            }
            if(minutes<10){
                minutes="0"+minutes;
            }
            if(seconds<10){
                seconds="0"+seconds;
            }
            return hours+":"+minutes+":"+seconds;
        },
        pause:function(){
            window.clearInterval(this.timerId);
        },
        resume:function(){
            var self=this;
            this.timerId=window.setInterval(function(){
                self.time++;
                self._render();
            },1000);

        },
        stop:function(){
            this.pause();
        }
    };
    window.Timer=Timer;
})();